layui.use(['form', 'jquery'], function () {
    var form = layui.form, $ = layui.jquery, player = parent.layer || layui.layer;

    var adminHomeUserInfoLogic = {
        //初始化
        init: function () {
            self = this;

            ///全局参数
            this.Parameters = {
                uploadImgUrl: "/Admin/Home/UploadImg",
                userInfoUrl: "/Admin/Home/LoadUserInfo",
                saveUserInfoUrl: "/Admin/Home/SaveUserInfo",
                postData: {}
            };
            this.initPage();

            this.bindEvent();
        },
        //初始化列表
        initPage: function () {
            GrdLayUI.ajaxPost({
                url: self.Parameters.userInfoUrl,
                data: "",
                success: function (result) {
                    if (result.Code == 1000) {
                        var userInfo = result.Data;
                        $(".loginName").val(userInfo.LoginName); //账号
                        $(".isSuperAdmin").val(userInfo.IsSuperAdmin == true ? "超级管理员" : "管理员"); //超级管理员
                        $(".roleCodeName").val(userInfo.RoleNames); //所属角色
                        $(".realName").val(userInfo.UserName); //用户名
                        $(".userSex input[value=" + userInfo.Sex + "]").attr("checked", "checked"); //性别
                        $(".userPhone").val(userInfo.Phone); //手机号
                        $("#userFace").attr("src", userInfo.PicUrl); //头像
                        form.render();
                    } else {
                        GrdLayUI.msg(result.Msg, "警告");
                    }
                }
            });
        },
        //绑定事件
        bindEvent: function () {
            //关闭
            $(".btn-close").on("click", this.logicFunc.close);

            //上传图片
            $(".userFaceBtn").on("click", this.logicFunc.uploadImg);

            //提交用户信息
            form.on('submit(changeUser)', function (data) {
                self.logicFunc.saveUserInfo();
            });
        },
        //事件
        logicFunc: {
            //上传图片
            uploadImg: function (obj) {

                //var pobj = $(obj.toElement);

                var callback_name = "loadUploadImgPage_callback_" + new Date().getTime();

                window[callback_name] = function (name, fullUrl) {
                   
                    //pobj.attr({ src: fullUrl + "?v=" + Math.random(), "data-savesrc": fullUrl });

                    //pobj.attr("src", fullUrl);

                    $(".userFaceBtn").attr({ src: fullUrl + "?v=" + Math.random(), "data-savesrc": fullUrl });
                };

                var index = layui.layer.open({
                    title: "图片上传",
                    type: 2,
                    resize: false,
                    area: ['820px', '650px'],
                    content: self.Parameters.uploadImgUrl+'?' + $.param({
                        imageType: "UserInfo",
                        callback: callback_name,
                        width: 500,
                        height: 350,
                        //isCropper: true
                    }),
                    end: function () {
                        delete window[callback_name];
                    }
                });
            },
            ///保存用户信息
            saveUserInfo: function () {
                self.Parameters.postData = GrdLayUI.initParamsData($("#edit-form"));
                var json = {
                    parameters: self.Parameters.postData
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveUserInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg('修改成功', "成功", function () {
                                //window.sessionStorage.setItem("userInfo", JSON.stringify(result.Data));
                                //var index = player.getFrameIndex(window.name);
                                //player.close(index);
                                //重新加载父页面
                                //window.parent.location.reload();
                                //form.render();
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            }
        },
        //工具
        tools: {

        }
    };
    adminHomeUserInfoLogic.init();
});
